package com.jingdianjichi.subject.infra.basic.service.utils;

import com.alibaba.druid.filter.config.ConfigTools;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

public class DruidEncrypUtil {
    private static String publicKey;
    private static String privateKey;
    static {
        try {
           String keyPair[]= ConfigTools.genKeyPair(512);
           privateKey=keyPair[0];
            System.out.println("publicKey:"+privateKey);
           publicKey=keyPair[1];
            System.out.println("privateKey:"+publicKey);

        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchProviderException e) {
            throw new RuntimeException(e);
        }
    }
    public static String encrypt(String plainText) throws Exception {
        String encrypt= ConfigTools.encrypt(privateKey,plainText);
        System.out.println("encrypt:"+encrypt);
        return encrypt;
    }
    public static String decrypt(String encryptedText) throws Exception {
        String decrypt= ConfigTools.decrypt(publicKey,encryptedText);
        System.out.println("decrypt:"+decrypt);
        return decrypt;
    }

    public static void main(String[] args) throws Exception {
        System.out.println(encrypt("123456"));
    }
}
